package com.bases.cellItem
{
	import com.bases.TImage;
	import com.bases.TLable;
	import com.bases.TRadioButton;
	import com.bases.TUIComponent;
	import com.Event.ImageDataEvent;
	import com.Event.RadioSelectEvent;
	import com.Event.RelayOutEvent;
	import com.Interface.IItem;
	import com.Interface.IRadioCellRender;
	import com.Interface.ExamIRadioData;
	
	import flash.events.MouseEvent;
	import flash.text.TextFormat;
	
	public class TRadioImageCellRender extends TUIComponent implements IItem,IRadioCellRender
	{
		private var radio:TRadioButton;
		private var text:TLable;
		private var _data:*;
		private var img:TImage;
		private var IMAGE_MAX_WIDTH:Number=300;
		private var IMAGE_MAX_HEIGHT:Number=150;
		public function TRadioImageCellRender()
		{
			super();
			this.radio=new TRadioButton();
			img=new TImage(false);
			this.text=new TLable();
			this.addChilds(text,radio,img);
			this.relayout();
			this.addEventListener(MouseEvent.CLICK,onClick);
		}
		
		private function onClick(event:MouseEvent):void{
			if(!this.radio.select){
				this.radio.select=true;
				this.dispatchEvent(new RadioSelectEvent(RadioSelectEvent.RADIO_SELECT_EVENT,this.radio,true));
			}
		}
		
		public function set radioText(value:String):void{
			this.text.text=value;
			this.adaptWidth();
		}
		
		public function set select(value:Boolean):void{
			this.radio.select=value;
		}
		
		public function get select():Boolean{
			return this.radio.select;
		}
		
		override protected function relayout():void{
			super.relayout();
			this.radio.y=5;
			this.text.setPos(20,0);	
			this.img.setPos(40,0);
			var scale:Number=(IMAGE_MAX_WIDTH/this.img.width<IMAGE_MAX_HEIGHT/this.img.height)
				?IMAGE_MAX_WIDTH/this.img.width:IMAGE_MAX_HEIGHT/this.img.height;
			if(scale<1){
				this.img.width=scale*this.img.width;
				this.img.height=scale*this.img.height;
			}
			this.text.setSize(this.img.width-40,img.height-40);
			this.dispatchEvent(new RelayOutEvent());
		}
		
		public function setFontSize(size:uint,color:uint=0x000000):void{
			var font:TextFormat=new TextFormat(null,size,color);
			this.text.setTextFormat(font);
		}
		
		public function setLableSize(w:Number,h:Number):void{
			this.text.setSize(w,h);
			this.radio.y=this.text.height/3;
		}
		
		public function set userData(value:*):void{
			_data=value;
			this.text.text=(value as ExamIRadioData).getID();
			this.setFontSize(20);
			this.adaptWidth();
			this.img.source=(value as ExamIRadioData).getContent();
			this.img.addEventListener(ImageDataEvent.IMAGE_LOAD_FINISH_EVENT,onImageLoadFinish);
		}
		
		private function onImageLoadFinish(event:ImageDataEvent):void{
			this.relayout();
		}
		
		private function adaptWidth():void{
			//this.text.setSize(this.text.textWidth+40,50);
			this.text.wordWrap=true;
		}
		
		public function get userData():*{
			return _data;
		}
	}
}